import path from 'path'
import RouteView from '@/layout/RouteView'
import sortBy from 'lodash/sortBy'
import arrayToTree from '@/utils/arrayToTree'

/* Layout */
import Layout from '@/layout'

// 前端路由表
const constantRouterComponents = {
  // 保育
  '/childCare': () => import('@/views/childCare'),
  // 日常保育
  '/childCare/daily': () => import('@/views/childCare/dailyCare'),
  '/childCare/daily/conservation': () =>
    import('@/views/childCare/dailyCare/tabs/conservation'), // 保育
  '/childCare/daily/bodyMeasure': () =>
    import('@/views/childCare/dailyCare/tabs/bodyMeasure'), // 体位测量
  '/childCare/daily/vaccination': () =>
    import('@/views/childCare/dailyCare/tabs/vaccination'), // 疫苗接种
  '/childCare/daily/babyReport': () =>
    import('@/views/childCare/dailyCare/tabs/babyDailyReport'), // 宝宝一日报告
  '/childCare/daily/attendanceTransfer': () =>
    import('@/views/childCare/dailyCare/tabs/attendanceTransfer'), // 考勤接送
  '/childCare/daily/attendance': () =>
    import('@/views/childCare/dailyCare/tabs/attendance'), // 考勤统计
  // 评测列表
  '/childCare/evaluation': () => import('@/views/childCare/evaluation'),
  '/childCare/evaluation/psychologicalBehavior': () =>
    import('@/views/childCare/evaluation/list'),
  '/childCare/evaluation/childrenHealth': () =>
    import('@/views/childCare/evaluation/list'),
  '/childCare/evaluation/psychologicalBehavior/info': () =>
    import('@/views/childCare/evaluation/detail/psychologicalBehavior'),
  '/childCare/evaluation/childrenHealth/info': () =>
    import('@/views/childCare/evaluation/detail/childrenHealth'),
  '/childCare/evaluation/sport': () => import('@/views/childCare/sport'),
  '/childCare/evaluation/sport/student': () =>
    import('@/views/childCare/sport/student'),
  '/childCare/evaluation/sport/classess': () =>
    import('@/views/childCare/sport/classess'),
  '/childCare/evaluation/sport/classess/info': () =>
    import('@/views/childCare/sport/classess/info'),
  // 营养食谱
  '/childCare/cookbook': () => import('@/components/Business/Tabs'),
  '/childCare/cookbook/brief': () =>
    import('@/views/childCare/cookbook/components/briefRecipe'),
  '/childCare/cookbook/recipe': () =>
    import('@/views/childCare/cookbook/components/tongBangRecipe'),

  // 运营
  '/operative': () => import('@/views/operate'),
  // // 数据统计
  // '/operative/dashboard': () => import('@/components/Business/Tabs'),
  // '/operative/dashboard/largeScreen': () => import('@/views/operate/dataStatics/largeScreen'),
  // '/operative/dashboard/group': () => import('@/views/operate/dataStatics/group'),
  // crm
  '/operative/crm': () => import('@/components/Business/Tabs'),
  '/operative/crm/product': () =>
    import('@/views/operate/saleCRM/tabs/product'),
  '/operative/crm/clue': () => import('@/views/operate/saleCRM/tabs/clue'),
  '/operative/crm/customer': () =>
    import('@/views/operate/saleCRM/tabs/customer'),
  '/operative/crm/heartClue': () =>
    import('@/views/operate/saleCRM/tabs/heartClue'),
  '/operative/crm/contract': () =>
    import('@/views/operate/saleCRM/tabs/contract'),
  '/operative/crm/clue/info': () =>
    import('@/views/operate/saleCRM/tabs/clue/infoDrawer'),
  '/operative/crm/heartClue/info': () =>
    import('@/views/operate/saleCRM/tabs/clue/infoDrawer'),
  '/operative/crm/customer/info': () =>
    import('@/views/operate/saleCRM/tabs/customer/infoDrawer'),
  '/operative/crm/contract/info': () =>
    import('@/views/operate/saleCRM/tabs/contract/infoDrawer'),
  // 档案管理
  '/operative/archives': () => import('@/views/operate/archives'),
  '/operative/archives/info': () => import('@/views/operate/archives/detail'),
  // 日常园务
  '/operative/dailyAffairs': () => import('@/views/operate/dailyAffairs'),
  '/operative/dailyAffairs/setting': () => import('@/components/Business/Tabs'),
  '/operative/dailyAffairs/setting/target': () =>
    import('@/views/operate/dailyAffairs/list'),
  '/operative/dailyAffairs/setting/type': () =>
    import('@/views/operate/dailyAffairs/list'),
  '/operative/dailyAffairs/setting/check': () =>
    import('@/views/operate/dailyAffairs/checkResult'),
  '/operative/dailyAffairs/setting/sterilization': () =>
    import('@/views/operate/dailyAffairs/sterilization'),
  // 数据统计
  '/operative/dashboard': () => import('@/components/Business/Tabs'),
  '/operative/dashboard/largeScreen': () =>
    import('@/views/operate/dashboard/largeScreen'),
  '/operative/dashboard/group': () => import('@/views/operate/dashboard/group'),
  '/operative/dashboard/gargen': () =>
    import('@/views/operate/dashboard/gargen'),

  // 教学
  '/educational': () => import('@/views/educational'),
  // 排课
  '/educational/curriculum': () => import('@/components/Business/Tabs'),
  '/educational/curriculum/toban': () =>
    import('@/views/educational/curriculum/schedule/toban'),
  '/educational/curriculum/family': () =>
    import('@/views/educational/curriculum/schedule/family'),
  '/educational/curriculum/early': () =>
    import('@/views/educational/curriculum/schedule/early'),
  '/teachManage/course/auditSetting': () =>
    import('@/views/educational/curriculum/auditSetting'),
  // 课程管理
  '/educational/courseManage': () => import('@/components/Business/Tabs'),
  '/educational/courseManage/course': () =>
    import('@/views/educational/courseManage/teachPlan'),
  '/educational/courseManage/course/info': () =>
    import('@/views/educational/courseManage/teachPlan/courseInfo'),
  '/educational/courseManage/course/detail': () =>
    import('@/views/educational/courseManage/teachPlan/courseDetail'),
  '/educational/courseManage/source': () =>
    import('@/views/educational/courseManage/sourceLibary'),
  '/educational/courseManage/source/info': () =>
    import('@/views/educational/courseManage/sourceLibary/detail'),
  '/educational/courseManage/courseInArea': () =>
    import('@/views/educational/courseManage/areaCustom'),

  // 成长观察
  '/childCare/growthObserve': () => import('@/components/Business/Tabs'),
  '/childCare/growthObserve/record': () =>
    import('@/views/educational/growthObserve/recordManagement'),
  '/childCare/growthObserve/publish': () =>
    import('@/views/educational/growthObserve/publish'),
  '/childCare/growthObserve/growingStory': () =>
    import('@/views/educational/growthObserve/growingStory'),
  '/childCare/growthObserve/milestone': () =>
    import('@/views/educational/growthObserve/milestone'),
  '/childCare/growthObserve/monthly': () =>
    import('@/views/educational/growthObserve/monthly'),
  '/childCare/growthObserve/ability': () =>
    import('@/views/educational/growthObserve/ability'),
  '/childCare/growthObserve/develop': () =>
    import('@/views/educational/growthObserve/develop'),

  // 财务
  '/finance': () => import('@/views/finance'),
  // 报表
  '/finance/report': () => import('@/components/Business/Tabs'),
  '/finance/report/cashier': () => import('@/views/finance/report'),
  '/finance/report/account': () => import('@/views/finance/report'),
  // 收费管理
  '/finance/charge': () => import('@/components/Business/Tabs'),
  '/finance/charge/plan': () => import('@/views/finance/charge/plan/index.vue'),
  '/finance/charge/plan/account/admin': () =>
    import('@/views/finance/charge/plan/account'),
  '/finance/charge/plan/account/admin/info': () =>
    import('@/views/finance/charge/plan/addCount'),
  // '/finance/charge/plan/account/unPay': () => import('@/views/finance/charge/plan/unPay'),
  '/finance/charge/record': () => import('@/views/finance/charge/record'),
  '/finance/charge/record/info': () =>
    import('@/views/finance/charge/record/components/addCount'),

  '/finance/charge/setting': () => import('@/views/finance/charge/setting'),
  '/finance/charge/setting/project': () =>
    import('@/views/operate/saleCRM/tabs/product'),
  '/finance/charge/setting/plan': () =>
    import('@/views/finance/charge/setting/plan'),
  '/finance/charge/setting/preferential': () =>
    import('@/views/finance/charge/setting/preferential'),
  '/finance/charge/setting/param': () =>
    import('@/views/finance/charge/setting/param'),
  // 退费
  '/finance/refund': () => import('@/components/Business/Tabs'),
  '/finance/refund/record': () => import('@/views/finance/refund/record'),
  '/finance/refund/settting': () => import('@/views/finance/refund/setting'),
  // 物资管理
  '/finance/material': () => import('@/components/Business/Tabs'),
  '/finance/material/list': () => import('@/views/finance/material/list'),
  '/finance/material/category': () =>
    import('@/views/finance/material/category'),
  '/finance/material/borrow': () => import('@/views/finance/material/borrow'),
  '/finance/material/record': () => import('@/views/finance/material/record'),
  // 备用金管理
  '/finance/imprest': () => import('@/components/Business/Tabs'),
  '/finance/imprest/record': () => import('@/views/finance/imprest/record'),

  // 师资
  '/teacherResource': () => import('@/views/teachers'),
  // 教保师培训
  '/teacherResource/training/live': () =>
    import('@/views/teachers/training/live/index.vue'),
  '/teacherResource/training': () => import('@/components/Business/Tabs'),
  '/teacherResource/training/record': () =>
    import('@/views/teachers/training/record'),
  '/teacherResource/training/source': () =>
    import('@/views/educational/courseManage/sourceLibary'),
  '/teacherResource/training/source/info': () =>
    import('@/views/educational/courseManage/sourceLibary/detail'),
  '/teacherResource/training/offline': () =>
    import('@/views/teachers/training/offline/index.vue'),
  // '/teacherResource/training/offline/detail': () => import('@/views/teachers/training/offline/detail.vue'),
  // 员工管理
  '/teacherResource/manage': () => import('@/views/teachers/staff/list'),
  '/teacherResource/manage/info': () => import('@/views/teachers/staff/detail'),

  // 基础配置
  // 班级管理
  '/setting': () => import('@/views/basicData'),
  '/setting/classes': () => import('@/views/basicData/class/list'),
  '/teachManage/classes/myClass': () => import('@/views/basicData/class/list'),
  '/teachManage/classes/classAdd': () => import('@/views/basicData/class/add'),
  // 教学区域管理
  '/setting/teachArea': () => import('@/views/basicData/teachArea'),
  // 学生管理
  '/setting/students': () => import('@/views/basicData/student/list'),
  // 权限管理
  '/setting/authority': () => import('@/components/Business/Tabs'),
  '/setting/authority/menu': () => import('@/views/basicData/authority/menu'),
  '/setting/authority/role': () => import('@/views/basicData/authority/role'),
  '/setting/authority/app': () => import('@/views/basicData/authority/app'),
  // 硬件管理
  '/setting/hardware': () => import('@/components/Business/Tabs'),
  '/setting/hardware/camera': () =>
    import('@/views/basicData/hardWare/tabs/camera'),
  '/setting/hardware/attendanceMachine': () =>
    import('@/views/basicData/hardWare/tabs/attendancemachine'),
  '/setting/hardware/iccard': () =>
    import('@/views/basicData/hardWare/tabs/iccard'),
  '/setting/hardware/iccard/student': () =>
    import('@/views/basicData/hardWare/tabs/iccard/student'),
  '/setting/hardware/iccard/parent': () =>
    import('@/views/basicData/hardWare/tabs/iccard/parent'),
  '/setting/hardware/iccard/teacher': () =>
    import('@/views/basicData/hardWare/tabs/iccard/teacher'),
  '/setting/hardware/iccard/query': () =>
    import('@/views/basicData/hardWare/tabs/iccard/query'),
  // 基础配置
  '/setting/config': () => import('@/components/Business/Tabs'),
  '/setting/config/document': () =>
    import('@/views/basicData/configure/document'),
  '/setting/config/course': () => import('@/views/basicData/configure/course'),

  // 销售crm
  '/sellCrm/clue': () => import('@/views/sellCrm/clue'), // 线索管理
  '/sellCrm/clue/info': () => import('@/views/sellCrm/component/clueInfo'), // 线索详情
  '/sellCrm/experimentalPallet': () =>
    import('@/views/sellCrm/experimentalPallet'), // 预约试托
  '/sellCrm/subscribeVisit': () => import('@/views/sellCrm/subscribeVisit'), // 预约到访

  // 师资管理
  '/teacherManagement/teacherFile': () =>
    import('@/views/teacherManagement/teacherFile.vue'), // 教师档案
  'teacherManagement/addTeacher': () =>
    import('@/views/teacherManagement/component/addTeacher.vue'), // 添加教师
  '/teacherManagement/performanceEvaluation': () =>
    import('@/views/teacherManagement/performanceEvaluation'), // 绩效评估
  '/educational/teacherManagement/liveBroadcast': () =>
    import('@/views/teacherManagement/liveBroadcast.vue'), // 直播/录播课程管理
  '/teacherManagement/teacherTraining': () =>
    import('@/views/teacherManagement/teacherTraining'), // 培训课程管理
  'teacherManagement/teacherTrainingInfo': () =>
    import('@/views/teacherManagement/component/teacherTrainingInfo.vue'), // 资源详情
  // 教务管理
  // 托班考勤
  '/educationalAdministration/clockingIn': () =>
    import('@/views/educationalAdministration/clocking-in'),
  // 请假管理
  '/educationalAdministration/LeaveManagement': () =>
    import('@/views/educationalAdministration/LeaveManagement'),
  // 活动通知
  '/educationalAdministration/eventNotification': () =>
    import('@/views/educationalAdministration/eventNotification'),
  // 教师点评
  '/educationalAdministration/teacherComments': () =>
    import('@/views/educationalAdministration/teacherComments'),
  // 宝宝管理
  '/baby/manage': () => import('@/views/baby/manage'),
  '/baby/vaccine': () => import('@/views/baby/vaccine'),
  '/baby/memorandum': () => import('@/views/baby/memorandum'),
  // 政府培训管理
  '/government/childcare': () => import('@/views/government/childcare'), // 保育人员
  '/government/director': () => import('@/views/government/director'), // 负责人
  '/government/directorDetail': () =>
    import('@/views/government/directorDetail'), // 负责人
  '/government/infoNotice': () => import('@/views/government/infoNotice'), // 信息通知
  '/government/emergency': () => import('@/views/government/emergency'), // 应急响应管理
  '/government/emergencyDetail': () =>
    import('@/views/government/emergencyDetail'), // 应急响应管理详情
  '/government/assessment': () => import('@/views/government/assessment'), // 质量评估
  '/government/assessmentSelf': () =>
    import('@/views/government/assessmentSelf'), // 质量评估机构自评
  '/government/assessmentDetail': () =>
    import('@/views/government/assessmentDetail'), // 质量评估详情
  '/government/subsidy': () => import('@/views/government/subsidy'),
  '/government/subsidyDetail': () => import('@/views/government/subsidyDetail'),
  '/government/rectification': () => import('@/views/government/rectification'),
  '/government/supervision': () => import('@/views/government/supervision'),
  '/coupon': () => import('@/views/coupon'),
  '/logOff': () => import('@/views/logOff'),
  '/recordFiling': () => import('@/views/recordFiling'),
  '/survey': () => import('@/views/survey'),
  '/report': () => import('@/views/report'),
  '/childCare/food': () => import('@/views/childCare/food')
}

const basicRouter = {
  path: '/',
  component: Layout,
  redirect: '/home',
  name: 'index',
  children: [
    {
      path: 'home',
      // component: () => import('@/views/childCare/dailyCare/tabs.vue'),
      component: () => import('@/views/home/index'),
      name: 'home',
      meta: { title: '首页', icon: 'dashboard', affix: true }
    },
    {
      path: '/survey/editDetail',
      component: () => import('@/views/survey/editDetail'),
      hidden: true
    },
    {
      path: '/AI',
      component: () => import('@/views/AI'),
      name: 'AI',
      meta: { title: 'AI问答', icon: 'dashboard', affix: true }
    },
    {
      path: '/logOff',
      component: () => import('@/views/logOff'),
      hidden: true
    },
    {
      path: '/logOff/add',
      component: () => import('@/views/logOff/add'),
      hidden: true
    },
    {
      path: '/logOff/detail',
      component: () => import('@/views/logOff/detail'),
      hidden: true
    },
    {
      path: '/childCare/food/add',
      component: () => import('@/views/childCare/food/add'),
      hidden: true
    },
    {
      path: '/childCare/food/detail',
      component: () => import('@/views/childCare/food/detail'),
      hidden: true
    },
    {
      path: '/coupon/detail',
      component: () => import('@/views/coupon/detail'),
      hidden: true
    },
    {
      path: '/government/subsidyAdd',
      component: () => import('@/views/government/subsidyAdd'),
      hidden: true
    },
    {
      path: '/report/addInfo',
      component: () => import('@/views/report/addInfo'),
      hidden: true
    },
    {
      path: '/government/supervisionDetail',
      component: () => import('@/views/government/supervisionDetail'),
      hidden: true
    },
    {
      path: '/recordFiling/recordDetail',
      component: () => import('@/views/recordFiling/recordDetail'),
      hidden: true
    },
    {
      path: '/recordFiling/recordAdd',
      component: () => import('@/views/recordFiling/recordAdd'),
      hidden: true
    },
    {
      path: '/government/subsidyDetail',
      component: () => import('@/views/government/subsidyDetail'),
      hidden: true
    },
    {
      path: '/sellCrm/clue/info',
      component: () => import('@/views/sellCrm/component/clueInfo'),
      hidden: true
    },
    {
      path: '/government/directorDetail',
      component: () => import('@/views/government/directorDetail'),
      hidden: true
    },
    {
      path: '/government/emergencyDetail',
      component: () => import('@/views/government/emergencyDetail'),
      hidden: true
    },
    {
      path: '/government/rectificationEdit',
      component: () => import('@/views/government/rectificationEdit'),
      hidden: true
    },
    {
      path: '/government/rectificationDetail',
      component: () => import('@/views/government/rectificationDetail'),
      hidden: true
    },
    {
      path: '/government/assessmentSelf',
      component: () => import('@/views/government/assessmentSelf'),
      hidden: true
    },
    {
      path: '/government/assessmentDetail',
      component: () => import('@/views/government/assessmentDetail'),
      hidden: true
    },
    {
      path: 'teacherManagement/teacherTrainingInfo',
      component: () =>
        import('@/views/teacherManagement/component/teacherTrainingInfo.vue'),
      hidden: true
    },
    {
      path: '/teacherManagement/addTeacher',
      component: () =>
        import('@/views/teacherManagement/component/addTeacher.vue'),
      name: 'addTeacher',
      meta: { title: '添加教师', icon: 'dashboard', affix: true }
    },
    {
      // 教务管理详情
      path: '/educationalAdministration/clockingIn/info/:id/:date/:name',
      component: () =>
        import('@/views/educationalAdministration/clocking-in/info.vue'),
      name: 'clockingInInfo',
      meta: { title: '托班考勤详情', icon: 'dashboard', affix: true }
    },
    {
      // 活动通知详情
      path: '/educationalAdministration/eventNotification/info/:id',
      component: () =>
        import('@/views/educationalAdministration/eventNotification/info.vue'),
      name: 'clockingInInfo',
      meta: { title: '托班考勤详情', icon: 'dashboard', affix: true }
    },
    {
      // 教师点评
      path: '/educationalAdministration/teacherComments',
      component: () =>
        import('@/views/educationalAdministration/teacherComments'),
      name: 'teacherComments',
      meta: { title: '教师点评', icon: 'dashboard', affix: true }
    }
  ]
}

// 前端未找到页面路由（固定不用改）
const notFoundRouter = {
  path: '*',
  redirect: '/404',
  hidden: true
}

export const generatorDynamicRouter = (list) => {
  const childrenNav = []
  const tree = list.length
    ? arrayToTree(list, {
      id: 'menuId',
      pid: 'parentId',
      firstPid: '0'
    }).filter((x) => x.menuType !== 'M')
    : []
  // 根级菜单
  generatorRoutes(sortBy(tree, ['orderNum']), childrenNav)
  basicRouter.children = basicRouter.children.concat(childrenNav)
  return {
    addRoutes: [basicRouter, notFoundRouter],
    accessedRoutes: childrenNav
  }
}

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerMap
 * @param parent
 * @returns {*}
 */
export const generatorRoutes = (list, childrenNav) => {
  list.map((item) => {
    const {
      url,
      menuId,
      perms,
      visible,
      menuName,
      children,
      parentId,
      icon,
      target,
      menuType,
      orderNum
    } = item
    const currentRouter = {
      key: menuId,
      // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
      path: path.resolve('/', url),
      // 路由名称，建议唯一
      name: perms,
      hidden: visible === '1',
      // 该路由对应页面的 组件 :方案2 (动态加载)
      component: constantRouterComponents[path.resolve('/', url)] || RouteView,

      // meta: 页面标题, 页面权限(供指令权限用，可去掉)
      meta: {
        group: parentId === '0',
        title: menuName,
        icon,
        name: perms,
        pid: parentId,
        target,
        menuType,
        orderNum
      }
    }
    // 是否有子菜单，并递归处理
    if (children && children.length > 0) {
      // Recursion
      currentRouter.children = generatorRoutes(
        sortBy(children, ['orderNum']),
        [],
        currentRouter
      )
    }
    // 重定向, 判断是否有子路由，并且当前路由没有对应组件
    if (
      item.target === 'menuBlank' &&
      item.children &&
      item.children.length > 0 &&
      item.children.every((x) => x.target === 'menuItem')
    ) {
      currentRouter.redirect = item.children[0].url
    }
    childrenNav.push(currentRouter)
  })
  return childrenNav
}
